fecha
Lightweight date formatting and parsing (~2KB). Meant to replace parsing and formatting functionality of moment.js.
NPM
npm install fecha --save
Yarn
yarn add fecha
Fecha vs Moment
| Fecha | Moment |
---|
Size (Min. and Gzipped) | 2.1KBs | 13.1KBs |
Date Parsing | ✓ | ✓ |
Date Formatting | ✓ | ✓ |
Date Manipulation | | ✓ |
I18n Support | ✓ | ✓ |
Use it
Formatting
format
accepts a Date object (or timestamp) and a string format and returns a formatted string. See below for
available format tokens.
Note: format
will throw an error when passed invalid parameters
import { format } from 'fecha';
type format = (date: Date, format?: string, i18n?: I18nSettings) => str;
format(new Date(2015, 10, 20), 'dddd MMMM Do, YYYY');
format(new Date(1998, 5, 3, 15, 23, 10, 350), 'YYYY-MM-DD hh:mm:ss.SSS A');
format(new Date(2015, 10, 20), 'isoDate');
format(new Date(2015, 10, 20), 'mediumDate');
format(new Date(2015, 10, 20, 3, 2, 1), 'isoDateTime');
format(new Date(2015, 2, 10, 5, 30, 20), 'shortTime');
format(new Date(2001, 2, 5, 6, 7, 2, 5), '[on] MM-DD-YYYY [at] HH:mm');
Parsing
parse
accepts a Date string and a string format and returns a Date object. See below for available format tokens.
NOTE: parse
will throw an error when passed invalid string format or missing format. You MUST specify a format.
import { parse } from 'fecha';
type parse = (dateStr: string, format: string, i18n?: I18nSettingsOptional) => Date|null;
parse('February 3rd, 2014', 'MMMM Do, YYYY');
parse('10-12-10 14:11:12', 'YY-MM-DD HH:mm:ss');
parse('5/3/98', 'shortDate');
parse('November 4, 2005', 'longDate');
parse('2015-11-20T03:02:01-05:00', 'isoDateTime');
parse('4 de octubre de 1983', 'M de MMMM de YYYY', {
monthNames: [
'enero',
'febrero',
'marzo',
'abril',
'mayo',
'junio',
'julio',
'agosto',
'septiembre',
'octubre',
'noviembre',
'diciembre'
]
});
i18n Support
import {setGlobalDateI18n} from 'fecha';
setGlobalDateI18n({
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
amPm: ['am', 'pm'],
DoFn: function (D) {
return D + [ 'th', 'st', 'nd', 'rd' ][ D % 10 > 3 ? 0 : (D - D % 10 !== 10) * D % 10 ];
}
});
Custom Named Masks
import { format, setGlobalDateMasks } from 'fecha';
setGlobalDateMasks({
myMask: 'HH:mm:ss YY/MM/DD';
});
format(new Date(2014, 5, 6, 14, 10, 45), 'myMask');
Formatting Tokens
| Token | Output |
---|
Month | M | 1 2 ... 11 12 |
| MM | 01 02 ... 11 12 |
| MMM | Jan Feb ... Nov Dec |
| MMMM | January February ... November December |
Day of Month | D | 1 2 ... 30 31 |
| Do | 1st 2nd ... 30th 31st |
| DD | 01 02 ... 30 31 |
Day of Week | d | 0 1 ... 5 6 |
| ddd | Sun Mon ... Fri Sat |
| dddd | Sunday Monday ... Friday Saturday |
Year | YY | 70 71 ... 29 30 |
| YYYY | 1970 1971 ... 2029 2030 |
AM/PM | A | AM PM |
| a | am pm |
Hour | H | 0 1 ... 22 23 |
| HH | 00 01 ... 22 23 |
| h | 1 2 ... 11 12 |
| hh | 01 02 ... 11 12 |
Minute | m | 0 1 ... 58 59 |
| mm | 00 01 ... 58 59 |
Second | s | 0 1 ... 58 59 |
| ss | 00 01 ... 58 59 |
Fractional Second | S | 0 1 ... 8 9 |
| SS | 0 1 ... 98 99 |
| SSS | 0 1 ... 998 999 |
Timezone | Z |
-07:00 -06:00 ... +06:00 +07:00
|
| ZZ |
-0700 -0600 ... +0600 +0700
|